﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo.Oracle;
using StockTrader.BusinessService.DataContract.HnxInfo;
using System.Data.OleDb;
using System.Data;
using StockTrader.DAL.OracleHelper;

namespace StockTrader.DAL.Oracle.HnxInfo
{
    public class OHnxOrder : IOHnxOrder
    {
        private OleDbConnection _internalConnection;
        private OleDbTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new OleDbConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters
        #endregion

        #region SQL string
        private const string GET_HNX_ALLORDERS = "SELECT * FROM STS_ORDERS WHERE NORC = '2'";
        //private const string GET_HNX_ALLORDERS = "SELECT * FROM STS_ORDERS";

        private const string GET_HNX_LASTORDERS = "SELECT * FROM STS_ORDERS WHERE NORC = '2' AND ORDER_TIME>?";
        //private const string GET_HNX_LASTORDERS = "SELECT * FROM STS_ORDERS WHERE ORDER_TIME>?";

        private const string COUNT_HNX_ORDERS = "COUNT (*) FROM STS_ORDERS WHERE NORC = '2'";
        #endregion

        #region IOHnxOrder Members

        public List<HnxOrderData> getOHnxAllOrders()
        {
            List<HnxOrderData> list = new List<HnxOrderData>();
            try
            {
                OleDbDataReader rdr = OleDbHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_ALLORDERS);
                while (rdr.Read())
                {
                    list.Add(new HnxOrderData(
                            Convert.ToDecimal(rdr["ORDER_ID"]),
                            rdr["ORDER_NO"].ToString(),
                            Convert.ToDateTime(rdr["ORDER_DATE"]),
                            rdr["ORDER_TIME"].ToString(),
                            Convert.ToDecimal(rdr["MEMBER_ID"]),
                            Convert.ToDecimal(rdr["STOCK_ID"]),
                            Convert.ToDecimal(rdr["ORDER_TYPE"]),
                            Convert.ToDecimal(rdr["OORB"]),
                            Convert.ToDecimal(rdr["NORP"]),
                            Convert.ToDecimal(rdr["NORC"]),
                            Convert.ToDecimal(rdr["SETTLEMENT_TYPE"]),
                            (rdr["DORF"] != DBNull.Value) ? Convert.ToDecimal(rdr["DORF"]) : 0,
                            Convert.ToDecimal(rdr["ORDER_QTTY"]),
                            Convert.ToDecimal(rdr["ORDER_PRICE"]),
                            Convert.ToDecimal(rdr["STATUS"]),
                            Convert.ToDecimal(rdr["QUOTE_PRICE"]),
                            Convert.ToDecimal(rdr["QUOTE_QTTY"]),
                            Convert.ToDecimal(rdr["EXEC_QTTY"]),
                            Convert.ToDecimal(rdr["CORRECT_QTTY"]),
                            Convert.ToDecimal(rdr["CANCEL_QTTY"]),
                            rdr["ACCOUNT_NO"].ToString(),
                            Convert.ToDecimal(rdr["BROKER_ID"])));
                }
                rdr.Close();

                return list;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public int countOHnxOrders()
        {
            List<HnxOrderData> list = new List<HnxOrderData>();
            try
            {
                return list.Count;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<HnxOrderData> getOHnxLastOrders(string time)
        {
            List<HnxOrderData> list = new List<HnxOrderData>();
            try
            {
                OleDbParameter parm1 = new OleDbParameter("ORDER_TIME", OleDbType.VarChar);
                parm1.Value = time;

                OleDbDataReader rdr = OleDbHelper.ExecuteReaderSingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_LASTORDERS, parm1);

                while (rdr.Read())
                {
                    list.Add(new HnxOrderData(
                            Convert.ToDecimal(rdr["ORDER_ID"]),
                            rdr["ORDER_NO"].ToString(),
                            Convert.ToDateTime(rdr["ORDER_DATE"]),
                            rdr["ORDER_TIME"].ToString(),
                            Convert.ToDecimal(rdr["MEMBER_ID"]),
                            Convert.ToDecimal(rdr["STOCK_ID"]),
                            Convert.ToDecimal(rdr["ORDER_TYPE"]),
                            Convert.ToDecimal(rdr["OORB"]),
                            Convert.ToDecimal(rdr["NORP"]),
                            Convert.ToDecimal(rdr["NORC"]),
                            Convert.ToDecimal(rdr["SETTLEMENT_TYPE"]),
                            (rdr["DORF"] != DBNull.Value) ? Convert.ToDecimal(rdr["DORF"]) : 0,
                            Convert.ToDecimal(rdr["ORDER_QTTY"]),
                            Convert.ToDecimal(rdr["ORDER_PRICE"]),
                            Convert.ToDecimal(rdr["STATUS"]),
                            Convert.ToDecimal(rdr["QUOTE_PRICE"]),
                            Convert.ToDecimal(rdr["QUOTE_QTTY"]),
                            Convert.ToDecimal(rdr["EXEC_QTTY"]),
                            Convert.ToDecimal(rdr["CORRECT_QTTY"]),
                            Convert.ToDecimal(rdr["CANCEL_QTTY"]),
                            rdr["ACCOUNT_NO"].ToString(),
                            Convert.ToDecimal(rdr["BROKER_ID"])));
                }
                rdr.Close();

                return list;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
